package net.hymnal.church;

import java.io.File;
import java.io.IOException;

import net.hymnal.church.HymnsManager.HYMNS_TYPE;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.util.DisplayMetrics;

public class Settings {
	private class SettingsNames {
		// public static final String SETTING_NAME = "hymns_settings_name";
		public static final String THEME_ID_NAME = "theme_id_name";
		public static final String TEXT_SIZE_NAME = "text_size_name";
		public static final String TEXT_LAST_COMMON_OLD_NUMER = "text_common_old_last_number";
		public static final String TEXT_LAST_COMMON_NEW_NUMER = "text_common_new_last_number";
		public static final String TEXT_LAST_YOUNG_NUMER = "text_young_last_number";		
	};

	public static final String splitSeparator = "|";

	static final class PathesClass {
		public static final String youngHymnalPath               = "young/";
		public static final String commonOldHymnalPath           = "common_gen/";
		public static final String commonOldHymnalMelodyPath     = "common_melody_gen/";
		public static final String commonOldHymnalTuneMelodyPath = "common_medody_tune_gen/";
		
		public static final String commonNewHymnalPath           = "common_new_gen/";
		public static final String commonNewHymnalMelodyPath     = "common_new_melody_gen/";
		public static final String commonNewHymnalTuneMelodyPath = "common_new_medody_tune_gen/";
		
		public static final String tableCommonPath               = "tables/";
		public static final String tableNewHymnsPath             = "tableHeader.db";
        public static       String dataPath = "/data/data/";
		public static final String tableMelodyFileName = "table_common.txt";
	}

	static final int MAX_COMMON_OLD_HYMNS = 551;
	static final int MAX_COMMON_NEW_HYMNS = 801;
	static final int MAX_YOUNG_HYMNS = 186;

	public static final int buttonTextSize = 60;

	static class BackgoundColor {
		public static final int BACK_GROUND_BLACK = 0xBBAAAAAA;
		public static final int BACK_GROUND_WHITE = 0x44303099;

		public static final int BACK_GROUND_BLACK_TITLE = 0xDDAAAAAA;
		public static final int BACK_GROUND_WHITE_TITLE = 0x88303099;
	}

	static class TextSettings {
		public static final int DEF_TEXT_SIZE = 24;
		public static final int MAX_TEXT_HYMN_SIZE = 80;
		public static final int MIN_TEXT_HYMN_SIZE = 12;
	}

	final int DEF_COMMON_LAST_HYMN = 0;
	final int DEF_YOUNG_LAST_HYMN = 0;
	final int DEF_THEME_ID = android.R.style.Theme_Light;

	public int mTextSize;
	public int themeID;
	boolean themeColorWhite = false;

	public Settings() {
		mTextSize = TextSettings.DEF_TEXT_SIZE;
		themeID = DEF_THEME_ID;
	}

	void loadSetting(Activity a) throws IOException {
		
		SharedPreferences settings = a.getPreferences(Context.MODE_PRIVATE);

		themeID = settings.getInt(SettingsNames.THEME_ID_NAME, DEF_THEME_ID);
		if (themeID != android.R.style.Theme_Light
				&& themeID != android.R.style.Theme_Black) {
			themeID = android.R.style.Theme_Light;
		}
		mTextSize = settings.getInt(SettingsNames.TEXT_SIZE_NAME,
				TextSettings.DEF_TEXT_SIZE);
		if (mTextSize < TextSettings.MIN_TEXT_HYMN_SIZE
				|| mTextSize > TextSettings.MAX_TEXT_HYMN_SIZE) {
			mTextSize = TextSettings.DEF_TEXT_SIZE;
		}
		int lastCommonOldNumber =  settings.getInt(SettingsNames.TEXT_LAST_COMMON_OLD_NUMER, DEF_COMMON_LAST_HYMN);
		HymnsManager.setLastSelectedHymn(HYMNS_TYPE.TYPE_COMMON_OLD, lastCommonOldNumber);
		
		int lastCommonNewNumber =  settings.getInt(SettingsNames.TEXT_LAST_COMMON_NEW_NUMER, DEF_COMMON_LAST_HYMN);
		HymnsManager.setLastSelectedHymn(HYMNS_TYPE.TYPE_COMMON_NEW, lastCommonNewNumber);
		
		int lastYoungNumber =  settings.getInt(SettingsNames.TEXT_LAST_YOUNG_NUMER, DEF_YOUNG_LAST_HYMN);
		HymnsManager.setLastSelectedHymn(HYMNS_TYPE.TYPE_YOUNG, lastYoungNumber);
		
		Settings.PathesClass.dataPath = "/data/data/" + a.getApplicationContext().getPackageName() + "/";
		System.out.println("dataPath = " + Settings.PathesClass.dataPath );
	}

	@SuppressLint("CommitPrefEdits")
	void saveSetting(Activity a) throws IOException {
		SharedPreferences settings = a.getPreferences(Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(SettingsNames.THEME_ID_NAME, themeID);
		editor.putInt(SettingsNames.TEXT_SIZE_NAME, mTextSize);
		int lastCommonOldNumber = HymnsManager.getLastSelectedHymn(HYMNS_TYPE.TYPE_COMMON_OLD);
		editor.putInt(SettingsNames.TEXT_LAST_COMMON_OLD_NUMER, lastCommonOldNumber);
		int lastCommonNewNumber = HymnsManager.getLastSelectedHymn(HYMNS_TYPE.TYPE_COMMON_NEW);
		editor.putInt(SettingsNames.TEXT_LAST_COMMON_NEW_NUMER, lastCommonNewNumber);
		int lastYoungNumber = HymnsManager.getLastSelectedHymn(HYMNS_TYPE.TYPE_YOUNG);
		editor.putInt(SettingsNames.TEXT_LAST_YOUNG_NUMER, lastYoungNumber);
		editor.commit();
	}

	void switchTheme() {
		if (themeID == android.R.style.Theme_Light) {
			themeID = android.R.style.Theme_Black;
		} else {
			themeID = android.R.style.Theme_Light; 
		}
	}

	public int getCurrentTheme() {
		return themeID;
	}

	public boolean isBlack() {
		if (themeID == android.R.style.Theme_Light) {
			return false;
		}
		return true;
	}
    
	static float defTextSize = (float)-1.0;
	
	static float getDefualtTextSize(Activity a) {
		if(defTextSize > 0.0) {
			return defTextSize;
		}
		double screenInches = getScreenInches(a);
		
		if (screenInches < 6.0) {
			defTextSize = (float)(24.0 * 1.5);
		}
		if (screenInches >= 6.0 && screenInches < 9.0) {
			defTextSize = (float)(24.0 * 2.0);
		}
		if (screenInches >= 9.0) {			
			defTextSize = (float)(24.0 * 3.0);
		}
		return defTextSize;
	}
	
	static int oneLinesLayout = -1;

	static public int getOneLinesRowLayout(Activity a) {
		if (oneLinesLayout > 0) {
			return oneLinesLayout;
		}
		double screenInches = getScreenInches(a);
		if (screenInches < 6.0) {
			oneLinesLayout = R.layout.one_lines_tiny;
		}
		if (screenInches >= 6.0 && screenInches < 9.0) {
			oneLinesLayout = R.layout.one_lines_medium;
		}
		if (screenInches >= 9.0) {
			oneLinesLayout = R.layout.one_lines_huge;
		}
		return oneLinesLayout;
	}

	static int twoLinesLayout = -1;

	static public int getTwoLinesRowLayout(Activity a) {
		if (twoLinesLayout > 0) {
			return twoLinesLayout;
		}
		double screenInches = getScreenInches(a);
		if (screenInches < 6.0) {
			twoLinesLayout = R.layout.two_lines_tiny;
		}
		if (screenInches >= 6.0 && screenInches < 9.0) {
			twoLinesLayout = R.layout.two_lines_medium;
		}

		if (screenInches >= 9.0) {
			twoLinesLayout = R.layout.two_lines_huge;
		}
		return twoLinesLayout;
	}

	static double screenInches = -1.0;

	static private double getScreenInches(Activity a) {
		if (screenInches > 0.0) {
			return screenInches;
		}
		DisplayMetrics dm = new DisplayMetrics();
		a.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels;
		int height = dm.heightPixels;
		int dens = dm.densityDpi;
		double wi = (double) width / (double) dens;
		double hi = (double) height / (double) dens;
		double x = Math.pow(wi, 2);
		double y = Math.pow(hi, 2);
		screenInches = Math.sqrt(x + y);
		return screenInches;
	}
	
	static String getAssetTableFilename() {
		return PathesClass.tableCommonPath + PathesClass.tableNewHymnsPath;
	}
	
	static String getDataTableFilename() {
		String str = PathesClass.dataPath + PathesClass.tableNewHymnsPath;
		System.out.println("data table path" + str);
		return str;
	}
}
